﻿// <copyright file="MonitorService.cs" company="Clever Software">
// Copyright (c) 2013, 2014 All Right Reserved, http://www.cleversoftware.by
//
// This source is subject to the Microsoft Permissive License.
// Please see the License.txt file for more information.
// All other rights reserved.
//
// THIS CODE AND INFORMATION ARE PROVIDED "AS IS" WITHOUT WARRANTY OF ANY 
// KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE
// IMPLIED WARRANTIES OF MERCHANTABILITY AND/OR FITNESS FOR A
// PARTICULAR PURPOSE.
//
// </copyright>
// <author>Clever Software</author>
// <email>info@cleversoftware.by</email>
// <date>2014-02-04</date> 
// <summary>CleverCMS source code</summary>

namespace CleverCMS.Cms.Services
{
    using System.Linq;
    using System.Collections.Generic;

    using CleverCMS.Cms.Data;
    using CleverCMS.Cms.Data.Entities;

    public class MonitorService
    {
        private readonly ICmsGenericRepository<CmsLog> _mLogsRepo;

        public MonitorService(ICmsGenericRepository<CmsLog> mLogsRepo)
        {
            _mLogsRepo = mLogsRepo;
        }

        #region Logs

        public IQueryable<CmsLog> LoadAll() 
        {
            return _mLogsRepo.LoadAll();
        }

        public IQueryable<CmsLog> LoadAll(int start, int size, string sortOrder, out int count)
        {
            var logs = _mLogsRepo.LoadAll(orderBy: q => q.OrderByDescending(d => d.Date))
                                                .Skip(start)
                                                .Take(size);
            count = _mLogsRepo.Count();

            return logs;
        }

        public IQueryable<CmsLog> LoadTopFiveErrors()
        {
            return _mLogsRepo.LoadAll().OrderByDescending(x => x.Date).Take(5);
        } 

        #endregion
    }
}